﻿using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using JTTD.Common.Attribute;
using JTTD.Domain;
using JTTD.Domain.Dtos;
using JTTD.Domain.Dtos.Activity.Request;
using JTTD.Domain.Dtos.Activity.Response;
using JTTD.Domain.Dtos.ShopActivity.Request;
using JTTD.Domain.Dtos.ShopActivity.Response;
using JTTD.Domain.IService;
using JTTD.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace JTTD.Controllers
{
    /// <summary>
    /// 店铺活动
    /// </summary>
    [Route("api/shopActivity")]
    [ApiController]
    public class ShopActivityController : BaseController
    {
        private readonly IShopActivityService _shopActivityService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="shopActivityService"></param>
        public ShopActivityController(IShopActivityService shopActivityService)
        {
            _shopActivityService = shopActivityService;
        }


        /// <summary>
        /// 获取单个店铺活动(店家后台)
        /// </summary>
        /// <param name="shopActivityId">活动id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{shopActivityId}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<ShopActivityInfoResponseDto>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Shop)]
        public async Task<IActionResult> GetShopActivity([FromRoute]int shopActivityId)
        {
            var result = await _shopActivityService.GetShopActivity(shopActivityId);
            return Ok(result);
        } 

        /// <summary>
        /// 分页获取店铺活动列表(店家后台)
        /// </summary>
        /// <param name="dto">店铺活动搜索条件dto</param>
        /// <returns></returns>
        [HttpGet]
        [Route("")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<PageResult<ShopActivityInfoResponseDto>>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Shop)]
        public async Task<IActionResult> GetShopActivityPageList([FromQuery]SearchShopActivityRequestDto dto)
        {
            var result = await _shopActivityService.GetShopActivityPageList(UserId,dto);
            return Ok(result);
        }


        /// <summary>
        /// 获取所有进行中的店铺活动(微信公众号)
        /// </summary>
        /// <returns></returns>
        [UnAuthorize]
        [HttpGet]
        [Route("{shopId}/enableActivitys")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<List<ShopActivityInfoResponseDto>>))]
        public async Task<IActionResult> GetEnableActivityList([FromRoute]int shopId)
        {
            var result = await _shopActivityService.GetEnableShopActivityList(shopId);
            return Ok(result);
        }

        /// <summary>
        /// 设置店铺活动（店家后台）
        /// </summary>
        /// <param name="dto">设置活动dto</param>
        /// <returns></returns>
        [HttpPost]
        [Route("")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Shop)]
        public async Task<IActionResult> SetShopActivity([FromBody]SetShopActivityRequestDto dto)
        {
            var result = await _shopActivityService.SetShopActivity(UserId,dto);
            return Ok(result);
        }

        /// <summary>
        /// 更改启用状态（店家后台）
        /// </summary>
        /// <param name="shopActivityId">活动id</param>
        /// <param name="isEnable">是否启用</param>
        /// <returns></returns>
        [HttpPut]
        [Route("{shopActivityId}/{isEnable}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Shop)]
        public async Task<IActionResult> ChangeEnable([FromRoute]int shopActivityId, [FromRoute]bool isEnable)
        {
            var result = await _shopActivityService.ChangeEnable(shopActivityId, isEnable);
            return Ok(result);
        }

        /// <summary>
        /// 删除店铺活动（店家后台）
        /// </summary>
        /// <param name="shopActivityId">活动id</param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{shopActivityId}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Shop)]
        public async Task<IActionResult> DeleteActivity([FromRoute]int shopActivityId)
        {
            var result = await _shopActivityService.DeleteShopActivity(shopActivityId);
            return Ok(result);
        }

        /// <summary>
        /// 参与店铺活动（微信公众号）
        /// </summary>
        /// <param name="shopActivityId"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("{shopActivityId}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Ordinary)]
        public async Task<IActionResult> JoinShopActivity([FromRoute]int shopActivityId)
        {
            var result = await _shopActivityService.JoinShopActivity(UserId,shopActivityId);
            return Ok(result);
        }



    }
}